#define  _CRT_SECURE_NO_WARNINGS

//#include <iostream>
//#include <string>
//using namespace std;
//
//int main()
//{
//	string s = "are you ok?";
//    size_t pos1 = 0;
//    size_t pos2 = s.find(" ", pos1);
//    while (pos2 < s.size())
//    {
//        reverse(s.begin() + pos1, s.begin() + pos2);
//        pos1 = pos2 + 1;
//        pos2 = s.find(" ", pos1);
//    }
//    reverse(s.begin() + pos1, s.end());
//    cout << s << endl;
//	return 0;
//}

#include <iostream>
#include <assert.h>
using namespace std;

namespace yjz
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		//string()
		//	:_str(new char[1]{'\0'})
		//	,_size(0)
		//	,_capacity(0)
		//{}

		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		void swap(string& str)
		{
			std::swap(_str, str._str);
			std::swap(_size, str._size);
			std::swap(_capacity, str._capacity);
		}

		//string(const string& str)
		//{
		//	_size = str._size;
		//	_capacity = str._capacity;
		//	_str = new char[_capacity + 1];
		//	strcpy(_str, str._str);
		//}

		string(const string& str)
		{
			string tmp(str._str);
			swap(tmp);
		}

		/*string& operator=(const string& str)
		{
			if (this != &str)
			{
				delete[] _str;
				_size = str._size;
				_capacity = str._capacity;
				_str = new char[_capacity + 1];
				strcpy(_str, str._str);
			}
			return *this;
		}*/

		string& operator=(string str)
		{
			swap(str);
			return *this;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		char& operator[](size_t i)
		{
			assert(i < _size);
			return _str[i];
		}

		const char& operator[](size_t i) const
		{
			assert(i < _size);
			return _str[i];
		}

		string& operator+=(char x)
		{
			push_back(x);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + size();
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + size();
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		const char* c_str() const
		{
			return _str;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				size_t length = _size + len > 2 * _capacity ? _size + len : 2 * _capacity;
				reserve(length);
			}
			strcpy(_str + _size, str);
			_size += len;
		}

		void push_back(char x)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			_str[_size++] = x;
			_str[_size] = '\0';
		}

		void insert(size_t pos, char ch)
		{
			assert(pos <= size());
			if (size() == capacity())
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			size_t end = size() + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= size());
			int length = strlen(str);
			if (size() + length > capacity())
			{
				reserve(size() + length > 2 * capacity() ? size() + length : 2 * capacity());
			}
			for (size_t i = size(); i >= pos; i--)
			{
				_str[i + length] = _str[i];
			}
			for (size_t i = 0; i < length; i++)
			{
				_str[i + pos] = *(str + i);
			}
			_size += length;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < size());
			if (len >= size() - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				for (size_t i = pos; i <= _size - len; i++)
				{
					_str[i] = _str[len + i];
				}
				_size -= len;
			}
		}

		size_t find(char ch, size_t pos = 0)
		{
			assert(pos < size());
			for (size_t i = pos; i < size(); i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
			return npos;
		}

		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < size());
			const char* ptr = strstr(_str + pos, str);
			if (ptr == nullptr)
			{
				return npos;
			}
			return ptr - _str;
		}

		string substr(size_t pos, size_t len) const
		{
			assert(pos < size());
			if (len >= size() - pos)
			{
				len = size() - pos;
			}
			string s;
			s.reserve(len);
			for (size_t i = 0; i < len; i++)
			{
				s += _str[pos + i];
			}
			return s;
		}

	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;

		static const size_t npos;
	};
	const size_t string::npos = -1;

	bool operator>(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) > 0;
	}
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}
	bool operator<(const string& s1, const string& s2)
	{
		return !(s1 > s2) && !(s1 == s2);
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return !(s2 > s2);
	}

	ostream& operator<<(ostream& out, const string& str)
	{
		for (auto e : str)
		{
			out << e;
		}
		return out;
	}

	/*istream& operator>>(istream& in, string& str)
	{
		str.clear();
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			str += ch;
			ch = in.get();
		}
		return in;
	}*/

	istream& operator>>(istream& in, string& str)
	{
		str.clear();
		const int N = 256;
		char buf[N];
		size_t i = 0;
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buf[i++] = ch;
			if (i == N - 1)
			{
				str[N - 1] = '\0';
				str += buf;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buf[i] = '\0';
			str += buf;
		}
		return in;
	}
}

namespace yjz
{
	void test_string1()
	{
		string s1;
		string s2("hello world");
		cout << s2.c_str() << endl;
		for (size_t i = 0; i < s2.size(); i++)
		{
			cout << s2[i] << " ";
		}
		cout << endl;

		string s3("hello world");
		string::iterator it = s3.begin();
		while (it != s3.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_string2()
	{
		string s1("hello world");
		s1.push_back('#');
		cout << s1.c_str() << endl;
		s1.append("Are you ok?");
		cout << s1.c_str() << endl;
		s1 += '%';
		cout << s1.c_str() << endl;
		s1 += "Are you ok?";
		cout << s1.c_str() << endl;
		s1.insert(1, '&');
		s1.insert(0, '*');
		cout << s1.c_str() << endl;
		s1.insert(6, "Are you ok?");
		cout << s1.c_str() << endl;
		s1.erase(1, 5);
		cout << s1.c_str() << endl;
	}

	void test_string3()
	{
		string s("hello world");
		size_t pos = s.find('l');
		string suffix = s.substr(pos, 6);
		cout << suffix.c_str() << endl;
		string s1("hello world");
		string s2 = s1;
		cout << s2.c_str() << endl;
		string s3("Are you ok?");
		s1 = s3;
		cout << s1.c_str() << endl;
	}

	void test_string4()
	{
		string s1("hello world");
		string s2;
		s2 = s1;
		cout << s1 << endl;
		cout << s2 << endl;
	}
}

int main()
{
	yjz::test_string1();
	yjz::test_string2();
	yjz::test_string3();
	yjz::test_string4();
	return 0;
}

